<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 智能食堂管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }
        html, body {
            height: 100%;
        }
        .container {
            height: 100%;
            background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .register-wrapper {
            background-color: #fff;
            width: 358px;
            height: 648px;
            border-radius: 15px;
            padding: 20px 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        .logo {
            width: 300px;
            height: auto;
            margin-bottom: 20px;
        }
        .header {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-bottom: 10px;
        }
        .input-item {
            display: block;
            width: 100%;
            margin-bottom: 20px;
            border: 0;
            padding: 10px;
            border-bottom: 1px solid rgb(128, 125, 125);
            font-size: 15px;
            outline: none;
        }
        .input-item::placeholder {
            text-transform: uppercase;
        }
        .btn {
            text-align: center;
            padding: 10px;
            width: 100%;
            margin-top: 20px;
            background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .btn:hover {
            opacity: 0.9;
        }
        .msg {
            text-align: center;
            margin-top: 20px;
        }
        a {
            text-decoration: none;
            color: #a6c1ee;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="register-wrapper">
            <img src="./images/cafeteria-logo.png" alt="Logo" class="logo">
            <div class="header">账号注册</div>
            <div class="form-wrapper">
                <input type="text" id="phone_number" name="phone_number" placeholder="手机号" class="input-item" required>
                <input type="password" id="password" name="password" placeholder="密码" class="input-item" required>
                <input type="password" id="confirm-password" name="confirm-password" placeholder="确认密码" class="input-item" required>
                <button class="btn" id="registerBtn">注册</button>
            </div>
            <div class="msg">
                已有账户？<a href="login.html">登录</a>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('registerBtn').addEventListener('click', function(event) {
            event.preventDefault();
            const phone_number = document.getElementById('phone_number').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            if (password !== confirmPassword) {
                alert('密码不匹配，请重新输入');
                return;
            }

            fetch('/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ phone_number, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('注册成功');
                    window.location.href = '/login.html'; // 注册成功后跳转到登录页面
                } else {
                    alert('注册失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('注册失败，请稍后重试。');
            });
        });
    </script>
</body>
</html>
